import { defineEventHandler, readBody } from 'h3'
import { recordLog } from '../util/log'

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event) as any
    const ua = (event.node?.req?.headers?.['user-agent'] || '') as string
    const pathFromClient = body?.path || ''

    const message = `client-route:${pathFromClient}; UA:${String(ua).slice(0, 500)}`

    await recordLog(event as any, {
      authority: 'access',
      message,
      username: undefined,
    })
  } catch (e) {
    try { console.warn && console.warn('[api/access-log] error', e) } catch (ee) { }
  }
  return { ok: true }
})
